package iscript.uiFramework.components.container
{
	import flash.display.DisplayObjectContainer;
	import flash.geom.Rectangle;
	import iscript.uiFramework.components.layout.ViewPortLayout;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.UIFramework;
	
	public class ViewPortBox extends UIComponent
	{
		
		private var _viewportLayout:ViewPortLayout;
		
		private var _northItem:UIComponent;
		private var _westItem:UIComponent;
		private var _centerItem:UIComponent;
		private var _eastItem:UIComponent;
		private var _southItem:UIComponent;
		
		private var _minBound:Rectangle;
		private var _bound:Rectangle;
		
		public function ViewPortBox(bound:Rectangle = null)
		{
			if (bound == null)
				bound = new Rectangle(0, 0, UIFramework.UiStage.stageWidth, UIFramework.UiStage.stageHeight);
				_bound = bound;
			_viewportLayout = new ViewPortLayout(_bound);
			_minBound = _viewportLayout.minRect;
			/*_viewportLayout.northHeight = 50;
			   _viewportLayout.westWidth = 50;
			   _viewportLayout.eastWidth = 50;
			 _viewportLayout.southHeight = 50;*/
			_viewportLayout.northHeight = 0;
			_viewportLayout.westWidth = 0;
			_viewportLayout.eastWidth = 0;
			_viewportLayout.southHeight = 0;
			super();
		}
		
		override protected function initialize():void
		{
			super.initialize();
		}
		
		/**
		 * 更新布局
		 * @param	bound
		 */
		public function update(bound:Rectangle = null):void
		{
			_viewportLayout.updateRect(bound);
			_elementUpdateLogic();
		}
		
		override public function render(container:DisplayObjectContainer = null):void
		{
			update();
			if (_northItem != null)
			{
				setChildIndex(_northItem, this.numChildren - 1);
			}
			super.render(container);
		}
		
		private function _elementUpdateLogic():void
		{
			if (_northItem != null && _viewportLayout.northHeight > 0)
			{
				_northItem.x = _viewportLayout.northRect.x;
				_northItem.y = _viewportLayout.northRect.y;
				_northItem.width = _viewportLayout.northRect.width;
				_northItem.height = _viewportLayout.northRect.height;
				_northItem.visible = true;
			}
			if (_northItem != null && _viewportLayout.northHeight <= 0)
			{
				_northItem.visible = false;
			}
			
			if (_westItem != null && _viewportLayout.westRect.width > 0)
			{
				_westItem.width = _viewportLayout.westRect.width;
				_westItem.height = _viewportLayout.westRect.height;
				_westItem.x = _viewportLayout.westRect.x;
				_westItem.y = _viewportLayout.westRect.y;
				_westItem.visible = true;
			}
			if (_westItem != null && _viewportLayout.westRect.width <= 0)
			{
				_westItem.visible = false;
			}
			
			if (_centerItem != null && _viewportLayout.centerRect.width > 0)
			{
				_centerItem.width = _viewportLayout.centerRect.width;
				_centerItem.height = _viewportLayout.centerRect.height;
				_centerItem.x = _viewportLayout.centerRect.x;
				_centerItem.y = _viewportLayout.centerRect.y;
				_centerItem.visible = true;
			}
			if (_centerItem != null && _viewportLayout.centerRect.width <= 0)
			{
				_centerItem.visible = false;
			}
			
			if (_eastItem != null && _viewportLayout.eastRect.width > 0)
			{
				_eastItem.width = _viewportLayout.eastRect.width;
				_eastItem.height = _viewportLayout.eastRect.height;
				_eastItem.x = _viewportLayout.eastRect.x;
				_eastItem.y = _viewportLayout.eastRect.y;
				_eastItem.visible = true;
			}
			if (_eastItem != null && _viewportLayout.eastRect.width <= 0)
			{
				_eastItem.visible = false;
			}
			
			if (_southItem != null && _viewportLayout.southRect.width > 0)
			{
				_southItem.width = _viewportLayout.southRect.width;
				_southItem.height = _viewportLayout.southRect.height;
				_southItem.x = _viewportLayout.southRect.x;
				_southItem.y = _viewportLayout.southRect.y;
				_southItem.visible = true;
			}
			if (_southItem != null && _viewportLayout.eastRect.width <= 0)
			{
				_southItem.visible = false;
			}
		}
		
		/* ====================================================================================
		 * Get Set
		 * ====================================================================================
		 */
		public function get northItem():UIComponent
		{
			return _northItem;
		}
		
		public function set northItem(value:UIComponent):void
		{
			value.render(this);
			_northItem = value;
			_northItem.width = _viewportLayout.northRect.width;
			if (_viewportLayout.northHeight == 0)
				_viewportLayout.northHeight = _northItem.height;
		}
		
		public function get westItem():UIComponent
		{
			return _westItem;
		}
		
		public function set westItem(value:UIComponent):void
		{
			value.render(this);
			_westItem = value;
			_westItem.width = _viewportLayout.westRect.width;
			_westItem.height = _viewportLayout.westRect.height;
			if (_viewportLayout.westWidth == 0)
				_viewportLayout.westWidth = _westItem.width;
		
		}
		
		public function get centerItem():UIComponent
		{
			return _centerItem;
		}
		
		public function set centerItem(value:UIComponent):void
		{
			value.render(this);
			_centerItem = value;
			_centerItem.width = _viewportLayout.centerRect.width;
			_centerItem.height = _viewportLayout.centerRect.height;
		}
		
		public function get eastItem():UIComponent
		{
			return _eastItem;
		}
		
		public function set eastItem(value:UIComponent):void
		{
			value.render(this);
			_eastItem = value;
			_eastItem.width = _viewportLayout.eastRect.width;
			_eastItem.height = _viewportLayout.eastRect.height;
			if (_viewportLayout.eastWidth == 0)
				_viewportLayout.eastWidth = _eastItem.width;
		}
		
		public function get southItem():UIComponent
		{
			return _southItem;
		}
		
		public function set southItem(value:UIComponent):void
		{
			value.render(this);
			_southItem = value;
			_southItem.width = _viewportLayout.southRect.width;
			_southItem.height = _viewportLayout.southRect.height;
			if (_viewportLayout.southHeight == 0)
				_viewportLayout.southHeight = _southItem.height;
		}
		
		/**
		 * 最小范围
		 */
		public function get minBound():Rectangle
		{
			return _minBound;
		}
		
		/**
		 * 最小范围
		 */
		public function set minBound(value:Rectangle):void
		{
			_minBound = value;
			_viewportLayout.minRect = value;
		}
		
		public function get Layout():ViewPortLayout
		{
			return _viewportLayout;
		}
		
		/**
		 * 北高度
		 */
		public function set northHeight(value:Number):void
		{
			_viewportLayout.northHeight = value;
			if (IsRender)
				update();
		
		}
		
		/**
		 * 南高度
		 */
		public function set southHeight(value:Number):void
		{
			_viewportLayout.southHeight = value;
			if (IsRender)
				update();

		}
		
		public function set westWidth(value:Number):void
		{
			_viewportLayout.westWidth = value;
			if (IsRender)
				update();
		
		}
		
		public function set eastWidth(value:Number):void
		{
			_viewportLayout.eastWidth = value;
			if (IsRender)
				update();
		}
		
		public function get bound():Rectangle 
		{
			return _bound;
		}
		
		public function set bound(value:Rectangle):void 
		{
			_bound = value;
			_viewportLayout.updateRect()
			if (IsRender)
			update();
		}
	
	}

}